package xpn.platform.modules.attachment;

import lombok.Getter;
import lombok.Setter;
import xpn.platform.modules.persistence.AbstractAuditorEntity;

import javax.persistence.Entity;
import javax.persistence.Table;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

@Entity
@Table(name = "common_attachment")
@Getter
@Setter
public class Attachment extends AbstractAuditorEntity {
	private static final long serialVersionUID = -7180582621654478382L;
	private String entityLink; // 所属实体的RESTful链接：/users/1
	private String category; // 分类
	private String categoryName; // 分类/分组名称
	private String filename; // 文件名
	public String fetchUrlParam(){
		String ret="1=1";
		if(!entityLink.isEmpty()){
			ret+="&entityLink="+entityLink;
		}
		if(!category.isEmpty()){
			ret+="&category="+category;
		}
		if(!filename.isEmpty()){
			ret+="&filename="+filename;
		}
		try {
			URLEncoder.encode(ret, "utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ret;
	}

	public String fetchPhysicalPath(){
		String path="";
		if(this.entityLink!=null&&!this.entityLink.equals("")){
			path+=path.equals("")?this.entityLink:"/"+this.entityLink;
		}
		if(this.category!=null&&!this.category.equals("")){
			path+=path.equals("")?this.category:"/"+this.category;
		}
		if(this.filename!=null&&!this.filename.equals("")){
			path+=path.equals("")?this.filename:"/"+this.filename;
		}
		return path;
	}

	public String fetchThumbnailUrlParam(){
		String ret="1=1";
		if(!entityLink.isEmpty()){
			ret+="&entityLink="+entityLink;
		}
		if(!category.isEmpty()){
			ret+="&category="+category;
		}
		if(!filename.isEmpty()){
			ret+="&filename="+filename+".thumbnail.jpg";
		}
		try {
			URLEncoder.encode(ret, "utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ret;
	}
}
